<?php
namespace app\circle\controller\admin;

use app\circle\model\DeptModel;
use think\Request;

class DeptController extends BaseController
{
    protected $deptModel;
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->deptModel=new DeptModel();
    }
    

    public function index()
    {
        $searchText=$this->request->param('searchText','');
        $map=[];
        if($this->adminInfo['admin_power']==2){
            $map['dept_id'] = $this->adminInfo['dept_id'];
        }
        if($this->adminInfo['admin_power']==3){
            $map['admin_id'] = $this->adminInfo['admin_id'];
        }
        $query=[];
        if(!empty($searchText)){
            $map['dept_name'] = ['like','%'.$searchText.'%'];
            $query['searchText'] = $searchText;
        }
        $list=$this->deptModel->where($map)->paginate(15,false,['query'=>$query]);
        $this->assign('list',$list);
        $this->assign('query',$query);
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()){
            $data=$this->request->param();
            if($data['dept_pid'] != 0){
                $deptList = $this->deptModel->getTree();
                $data['dept_level'] = $deptList[$data['dept_pid']]['dept_level'] +1;
                $data['dept_path'] = $deptList[$data['dept_pid']]['dept_path'];
            }else{
                $data['dept_path'] = "0";
            }
            $result=$this->validate($data,[
                'dept_name|部门名称' => 'require',
                'dept_trade|行业名称'  => 'require',
            ]);
            if (true!==$result){
                $this->error($result);
            }
            if ($this->deptModel->addData($data)){
                $deptId = $this->deptModel->dept_id;
                $deptPath = $data['dept_path'].",".$deptId;
                $this->deptModel->where('dept_id',$deptId)->setField('dept_path',$deptPath);
                $this->success('部门添加成功','index');
            }else{
                $this->error('部门添加失败');
            }
        }
        $this->assign('deptList',$this->deptModel->getTree());
        return $this->fetch();
    }

    public function edit()
    {
        $deptId=$this->request->param('dept_id',0);
        if ($this->request->isPost()){
            $data=$this->request->param();
            if($data['dept_pid'] != 0){
                $deptList = $this->deptModel->getTree();
                $data['dept_level'] = $deptList[$data['dept_pid']]['dept_level'] +1;
                $data['dept_path'] = $deptList[$data['dept_pid']]['dept_path'].",".$deptId;
            }else{
                $data['dept_path'] = "0,".$deptId;
            }
            $result=$this->validate($data,[
                'dept_name|部门名称' => 'require',
                'dept_trade|行业名称'  => 'require',
            ]);
            if (true!==$result){
                $this->error($result);
            }
            if ($this->deptModel->editData($data)){
                $this->success('部门修改成功','index');
            }else{
                $this->error('部门修改失败');
            }
        }
        $deptList = $this->deptModel->getTree();
        $info     = $deptList[$deptId];
        $list = [];
        foreach ($deptList as $k => $v) {
            //父栏目select状态
            $v['selected'] = $info['dept_id'] == $v['dept_id'] ? 'selected' : '';
            //子栏目disabled
            $disabled      = \org\util\TreeUtil::isChild($deptList, $v['dept_id'], $deptId, 'dept_id') || $deptId == $v['dept_pid'] ? 'disabled' : '';
            $v['disabled'] = $disabled;
            $list[]    = $v;
        }
        $this->assign('list',$list);
        $this->assign('info',$info);
        return $this->fetch();
    }


    public function del()
    {
        $deptId=$this->request->param('dept_id',0);
        if ($this->deptModel->where('dept_pid',$deptId)->find()){
            $this->error('有子部门，不能删','index');
        }else{
            $this->deptModel->destroy($deptId);
            $this->success('删除成功','index');
        }
    }

    /**
     *部门排序
     */
    public function updateOrder()
    {
        $listOrder = $this->request->param('list_order/a');
        foreach ($listOrder as  $deptId=> $deptSort) {
            $deptId     = intval($deptId);
            $deptSort   = intval($deptSort);
            $dataList[] = ["dept_id" => $deptId, "dept_sort" => $deptSort];
        }
        $this->deptModel->saveAll($dataList);
        $this->success('排序成功！');
    }
}